/**
 * problem_034.cpp
 * Copyright (C) 2011 - xrose
 */

#include <iostream>
#include <stdio.h>
using namespace std;

int factor(int n)
{
//    if(n == 0) return 0;
	if((0<=n)&&(n<2)) return 1;
	return n*factor(n-1);
}
int caldigit(const int abc)
{
	int sumf = 0;
	int ab, c;
	ab = abc;
	while(ab>0)
	{
		c = ab%10;
		sumf += factor(c);
		ab = (ab-c)/10;
	}
	return sumf;
}

int main (int argc, char const* argv[])
{
//	cout<<factor(9)<<endl;
//	cout<<caldigit(40585)<<endl;
    int sum = 0;
	for(int i = 3; i<=2540160; i++)
	{
//	    cout <<i<<endl;
	    if(caldigit(i) == i)
	    {
	        cout<<i<<endl;
	        sum+=i;
	    }
	}
	cout<<sum<<endl;
	return 0;
}
